{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mesh generation for tutorial 02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gmsh\n",
    "from mpi4py import MPI\n",
    "from dolfinx.io import XDMFFile\n",
    "from multiphenicsx.mesh.utils import gmsh_to_fenicsx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert MPI.COMM_WORLD.size == 1, \"This mesh generation notebook is supposed to be run in serial\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Geometrical parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pre_step_length = 4.\n",
    "after_step_length = 14.\n",
    "pre_step_height = 3.\n",
    "after_step_height = 5.\n",
    "lcar = 1. / 5."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create mesh with gmsh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gmsh.initialize()\n",
    "gmsh.model.add(\"mesh\")\n",
    "p0 = gmsh.model.geo.addPoint(0.0, after_step_height - pre_step_height, 0.0, lcar)\n",
    "p1 = gmsh.model.geo.addPoint(pre_step_length, after_step_height - pre_step_height, 0.0, lcar)\n",
    "p2 = gmsh.model.geo.addPoint(pre_step_length, 0.0, 0.0, lcar)\n",
    "p3 = gmsh.model.geo.addPoint(pre_step_length + after_step_length, 0.0, 0.0, lcar)\n",
    "p4 = gmsh.model.geo.addPoint(pre_step_length + after_step_length, after_step_height, 0.0, lcar)\n",
    "p5 = gmsh.model.geo.addPoint(0.0, after_step_height, 0.0, lcar)\n",
    "l0 = gmsh.model.geo.addLine(p0, p1)\n",
    "l1 = gmsh.model.geo.addLine(p1, p2)\n",
    "l2 = gmsh.model.geo.addLine(p2, p3)\n",
    "l3 = gmsh.model.geo.addLine(p3, p4)\n",
    "l4 = gmsh.model.geo.addLine(p4, p5)\n",
    "l5 = gmsh.model.geo.addLine(p5, p0)\n",
    "line_loop = gmsh.model.geo.addCurveLoop([l0, l1, l2, l3, l4, l5])\n",
    "domain = gmsh.model.geo.addPlaneSurface([line_loop])\n",
    "gmsh.model.geo.synchronize()\n",
    "gmsh.model.addPhysicalGroup(1, [l5], 1)\n",
    "gmsh.model.addPhysicalGroup(1, [l0, l1, l2, l4], 2)\n",
    "gmsh.model.addPhysicalGroup(2, [domain], 0)\n",
    "gmsh.model.mesh.generate(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert to a dolfinx mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh, subdomains, boundaries = gmsh_to_fenicsx(gmsh.model, gdim=2)\n",
    "gmsh.finalize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save mesh and boundaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with XDMFFile(MPI.COMM_WORLD, \"backward_facing_step.xdmf\", \"w\") as file:\n",
    "    file.write_mesh(mesh)\n",
    "    mesh.topology.create_connectivity(mesh.topology.dim - 1, mesh.topology.dim)\n",
    "    file.write_meshtags(boundaries)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython"
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
